let entryNotShow = false;

function createEntry (parent, hors='H', content='', note='Untitled') {
    let entry = document.createElement('div');
    entry.className = 'entry';
    entry.innerHTML = `<span>${hors}</span><span contenteditable="true">${content}</span><span contenteditable="false">${note}</span><span><svg xmlns="http://www.w3.org/2000/svg"><use xlink:href="../assets/plane.svg#plane"></use></svg></span>`;
    // window.addEventListener('resize', () => {
    //     if (entry.childNodes[1] > 350) {
    //     }
    // });
    /* note pad events */
    entry.childNodes[2].ondblclick = function () {
        this.setAttribute('contenteditable', 'true');
        this.focus();   /* focus on the DOM */
    }
    entry.childNodes[2].onblur = function () {
        this.setAttribute('contenteditable', 'false');
        this.scrollLeft = 0; /* reset the scroll */
    }
    /* hex or decimal pad */
    entry.childNodes[0].onclick = function () {
        if (entry.childNodes[0].innerText == 'H') {
            entry.childNodes[0].innerText = 'S';
            infoPrint('Send the data as STRING');
        } else if (entry.childNodes[0].innerText == 'S') {
            entry.childNodes[0].innerText = 'A';
            infoPrint('Send the data Automatically');
        } else {
            entry.childNodes[0].innerText = 'H';
            infoPrint('Send the data as HEX');
        }
    }
    /* send button */
    entry.childNodes[3].onclick = function () {
        if (ocom.com.isopen) {
            let text = entry.childNodes[1].innerText;
            if (text != '') {
                switch (entry.childNodes[0].innerText) {
                    case 'A':
                        if (isHex(text)) {
                            comWriteHex(text);
                            consoleAddS(ocom.console.doms.consoleMsgs, text);
                        } else {
                            comWriteStr(text);
                            consoleAddS(ocom.console.doms.consoleMsgs, text);
                        }
                        break;
                    case 'H':
                        if (isHex(text)) {
                            comWriteHex(text);
                            consoleAddS(ocom.console.doms.consoleMsgs, text);
                        } else {
                            infoPrint('Wrong Hex format of the data, e.g.(11 22 aa bb)', 0, 20);
                            playSendErrAni(entry);
                        }
                        break;
                    case 'S':
                        comWriteStr(text);
                        consoleAddS(ocom.console.doms.consoleMsgs, text);
                        break;
                }
            } else {
                infoPrint('Please input data in the textbox', 0, 20);
                playSendErrAni(entry);
            }
        } else {
            infoPrint('COM port not open');
            playSendErrAni(entry);
        }
    }
    parent.appendChild(entry);
}

function drawEntryPadByJson (parent, jsonStr) {

}

function drawEntryPadByNum (parent, num) {
    for (let i = 0; i < num; i++) {
        createEntry(parent, 'A', '', i.toString().padStart(3, '0'));
    }
}

function entryInit (index) {

}

/* FIXME: Excessive performance consumption !!! */
function playSendErrAni (entry) {
    entry.childNodes[1].style.animation = 'button-click 0.4s ease 0s 1 alternate forwards';
    entry.childNodes[1].addEventListener('animationend', () => {
        entry.childNodes[1].style.animation = '';
    }, { once: true }); /* `animationend` event once execute */
}